package com.fr.chart.core.glyph;

import com.fr.base.ArrayUtils;
import com.fr.base.FRFont;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.RectangleGlyph;
import com.fr.chart.legend.LegendItem;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class LegendGlyph extends RectangleGlyph
{
  private static final long serialVersionUID = 8315404889874481890L;
  public static final int MARGIN = 2;
  public static final String XML_TAG = "LegendGlyph";
  private LegendItem[] items;
  private FRFont font;
  private int position = 4;
  private boolean autoAdjustSize = false;

  public LegendGlyph()
  {
  }

  public LegendGlyph(LegendItem[] paramArrayOfLegendItem)
  {
    setItems(paramArrayOfLegendItem);
  }

  public void setItems(LegendItem[] paramArrayOfLegendItem)
  {
    this.items = paramArrayOfLegendItem;
  }

  public LegendItem[] getItems()
  {
    return this.items;
  }

  public void setFont(FRFont paramFRFont)
  {
    this.font = paramFRFont;
  }

  public FRFont getFont()
  {
    return this.font;
  }

  public void setPosition(int paramInt)
  {
    this.position = paramInt;
  }

  public int getPosition()
  {
    return this.position;
  }

  public void setRoundRect(boolean paramBoolean)
  {
    setRoundBorder(paramBoolean);
  }

  public boolean isRoundRect()
  {
    return isRoundBorder();
  }

  public Iterator selectableChildren()
  {
    return Collections.EMPTY_LIST.iterator();
  }

  public void draw(Graphics paramGraphics)
  {
    int i;
    super.draw(paramGraphics);
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Shape localShape = localGraphics2D.getClip();
    localGraphics2D.clip(getBounds());
    localGraphics2D.translate(this.bounds.getX(), this.bounds.getY());
    if (getItems() != null)
      for (i = 0; i < getItems().length; ++i)
        getItems()[i].draw(localGraphics2D, this.font);
    localGraphics2D.translate(-this.bounds.getX(), -this.bounds.getY());
    localGraphics2D.setClip(localShape);
  }

  public Dimension2D prefferedSize()
  {
    double d2;
    double d3;
    double d4;
    double d5;
    int j;
    Dimension2D localDimension2D;
    if (ArrayUtils.isEmpty(getItems()))
      return new Dimension();
    double d1 = 0.2D;
    int i = GraphHelper.getLineStyleSize(getBorderStyle());
    if ((this.position == 1) || (this.position == 3))
    {
      d4 = 0.0D;
      d5 = 0.0D;
      for (j = 0; j < getItems().length; ++j)
      {
        localDimension2D = getItems()[j].prefferedSize(this.font);
        d4 += localDimension2D.getWidth();
        d5 = Math.max(d5, localDimension2D.getHeight());
      }
      d2 = i * 2 + 4 * getItems().length + d4 * (d1 + 1.0D);
      d3 = 4.0D + Math.max(d5, this.font.getSize() * (1.0D + d1));
    }
    else
    {
      d4 = 0.0D;
      d5 = 0.0D;
      for (j = 0; j < getItems().length; ++j)
      {
        localDimension2D = getItems()[j].prefferedSize(this.font);
        d5 += localDimension2D.getHeight();
        d4 = Math.max(d4, localDimension2D.getWidth());
      }
      d3 = i * 2 + 4 * getItems().length + d5 * (1.0D + d1);
      d2 = (1.0D + d1) * d4 + 4.0D + i * 2;
    }
    return new Dimension((int)d2, (int)d3);
  }

  public void calculateBounds(Rectangle2D paramRectangle2D)
  {
    if (ArrayUtils.isEmpty(getItems()))
      return;
    this.font = this.font.applySize(1);
    for (Dimension2D localDimension2D = prefferedSize(); (localDimension2D.getHeight() < paramRectangle2D.getHeight()) && (localDimension2D.getWidth() < paramRectangle2D.getWidth()); localDimension2D = prefferedSize())
      this.font = this.font.applySize(this.font.getSize() + 1);
    if ((this.position == 1) || (this.position == 3))
      paramRectangle2D.setFrame(paramRectangle2D.getX() + (paramRectangle2D.getWidth() - localDimension2D.getWidth()) / 2.0D, paramRectangle2D.getY(), localDimension2D.getWidth(), localDimension2D.getHeight());
    else
      paramRectangle2D.setFrame(paramRectangle2D.getX(), paramRectangle2D.getY() + (paramRectangle2D.getHeight() - localDimension2D.getHeight()) / 2.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
  }

  public void refreshItemBounds()
  {
    if ((getItems() == null) || (getItems().length <= 0))
      return;
    FontMetrics localFontMetrics = GraphHelper.getFontMetrics(this.font);
    int i = localFontMetrics.getHeight();
    int j = GraphHelper.getLineStyleSize(getBorderStyle());
    double d1 = this.bounds.getWidth() - j * 2;
    double d2 = this.bounds.getHeight() - j * 2;
    int k = Math.max(i, this.font.getSize());
    double d3 = 0.0D;
    for (int l = 0; l < getItems().length; ++l)
      d3 += getItems()[l].prefferedSize(this.font).getWidth();
    if (d3 < d1)
    {
      d4 = (d1 - d3) / getItems().length;
      double d5 = Math.max(0.0D, d2 - k);
      d7 = 0.0D;
      int i3 = 0;
      while (true)
      {
        if (i3 >= getItems().length)
          return;
        double d9 = d4 / 2.0D + d7 + d4 * i3 + j;
        double d10 = d5 / 2.0D + j;
        getItems()[i3].setBounds(new Rectangle((int)d9, (int)d10, this.font.getSize(), this.font.getSize()));
        d7 += getItems()[i3].prefferedSize(this.font).getWidth();
        ++i3;
      }
    }
    double d4 = 0.0D;
    for (int i1 = 0; i1 < getItems().length; ++i1)
    {
      double d6 = getItems()[i1].prefferedSize(this.font).getWidth();
      d4 = Math.max(d6, d4);
    }
    if (d4 < 0.1D)
      return;
    i1 = Math.max(1, (int)(d1 / d4));
    int i2 = getItems().length / i1;
    if (i2 * i1 < getItems().length);
    double d7 = ??? / ++i2 - Math.max(i, this.font.getSize());
    d7 = Math.max(d7, 0.0D);
    double d8 = d1 / i1;
    for (int i4 = 0; i4 < getItems().length; ++i4)
    {
      int i5 = i4 % i1;
      int i6 = i4 / i1;
      double d11 = d8 * i5 + (d8 - d4) / 2.0D + j;
      double d12 = d7 / 2.0D + i6 * k + i6 * d7 + j;
      getItems()[i4].setBounds(new Rectangle((int)d11, (int)d12, this.font.getSize(), this.font.getSize()));
    }
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("FRFont"))
      {
        this.font = BaseXMLUtils.readFRFont(paramXMLableReader);
      }
      else if (str1.equals("Items"))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final LegendGlyph this$0;

          public void readXML()
          {
            if (paramXMLableReader.isChildNode())
            {
              String str = paramXMLableReader.getTagName();
              if (str.equals("LegendItem"))
              {
                LegendItem localLegendItem = (LegendItem)paramXMLableReader.readXMLObject(new LegendItem());
                if (LegendGlyph.access$000(this.this$0) == null)
                {
                  LegendGlyph.access$002(this.this$0, new LegendItem[] { localLegendItem });
                }
                else
                {
                  LegendItem[] arrayOfLegendItem = LegendGlyph.access$000(this.this$0);
                  LegendGlyph.access$002(this.this$0, new LegendItem[arrayOfLegendItem.length + 1]);
                  System.arraycopy(arrayOfLegendItem, 0, LegendGlyph.access$000(this.this$0), 0, arrayOfLegendItem.length);
                  LegendGlyph.access$000(this.this$0)[arrayOfLegendItem.length] = localLegendItem;
                }
              }
            }
          }
        });
      }
      else
      {
        String str2;
        if (str1.equals("Position"))
        {
          if ((str2 = paramXMLableReader.getAttr("value")) != null)
            setPosition(Integer.parseInt(str2));
        }
        else if (str1.equals("Attr"))
        {
          if ((str2 = paramXMLableReader.getAttr("position")) != null)
            setPosition(Integer.parseInt(str2));
          if ((str2 = paramXMLableReader.getAttr("autoAdjustSize")) != null)
            setAutoAdjustSize(Boolean.valueOf(str2).booleanValue());
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("LegendGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("position", this.position).attr("autoAdjustSize", this.autoAdjustSize).end();
    if (getFont() != null)
      BaseXMLUtils.writeFRFont(paramXMLPrintWriter, getFont());
    if ((getItems() != null) && (getItems().length > 0))
    {
      paramXMLPrintWriter.startTAG("Items");
      for (int i = 0; i < getItems().length; ++i)
        getItems()[i].writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof LegendGlyph))
      return false;
    LegendGlyph localLegendGlyph = (LegendGlyph)paramObject;
    if (!(super.equals(localLegendGlyph)))
      return false;
    if (!(Equals.equals(localLegendGlyph.font, this.font)))
      return false;
    if (localLegendGlyph.position != this.position)
      return false;
    if (localLegendGlyph.autoAdjustSize != this.autoAdjustSize)
      return false;
    if ((localLegendGlyph.getItems() != null) && (getItems() != null))
    {
      if (localLegendGlyph.getItems().length != getItems().length)
        return false;
      int i = 0;
      while (true)
      {
        if (i >= localLegendGlyph.getItems().length)
          break label162;
        if (!(Equals.equals(localLegendGlyph.getItems()[i], getItems()[i])))
          return false;
        ++i;
      }
    }
    label162: return ((((localLegendGlyph.getItems() != null) || (getItems() == null))) && (((localLegendGlyph.getItems() == null) || (getItems() != null))));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("position", this.position);
    localJSONObject.put("autoAdjustSize", this.autoAdjustSize);
    if (this.font != null)
      localJSONObject.put("font", this.font.toJSONObject());
    if ((this.items != null) && (this.items.length > 0))
    {
      JSONArray localJSONArray = new JSONArray();
      for (int i = 0; i < this.items.length; ++i)
        localJSONArray.put(this.items[i].toJSONObject());
      localJSONObject.put("items", localJSONArray);
    }
    return localJSONObject;
  }

  public void setAutoAdjustSize(boolean paramBoolean)
  {
    this.autoAdjustSize = paramBoolean;
  }

  public boolean isAutoAdjustSize()
  {
    return this.autoAdjustSize;
  }

  static LegendItem[] access$000(LegendGlyph paramLegendGlyph)
  {
    return paramLegendGlyph.items;
  }

  static LegendItem[] access$002(LegendGlyph paramLegendGlyph, LegendItem[] paramArrayOfLegendItem)
  {
    return (paramLegendGlyph.items = paramArrayOfLegendItem);
  }
}